<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>复杂计算器</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            background-color: #f4f4f4;
        }

        .calculator {
            background-color: #fff;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            padding: 20px;
        }

        #display {
            width: 100%;
            height: 50px;
            font-size: 24px;
            text-align: right;
            margin-bottom: 10px;
        }

        .buttons {
            display: grid;
            grid-template-columns: repeat(4, 1fr);
            grid-gap: 10px;
        }

        button {
            padding: 15px;
            font-size: 18px;
            border: none;
            background-color: #e0e0e0;
            cursor: pointer;
        }

        button:hover {
            background-color: #d0d0d0;
        }
    </style>
</head>

<body>
<div class="calculator">
    <input type="text" id="display" readonly>
    <div class="buttons">
        <button onclick="calculator.clearDisplay()">C</button>
        <button onclick="calculator.appendToDisplay('(')">(</button>
        <button onclick="calculator.appendToDisplay(')')">)</button>
        <button onclick="calculator.appendToDisplay('/')">/</button>
        <button onclick="calculator.appendToDisplay('7')">7</button>
        <button onclick="calculator.appendToDisplay('8')">8</button>
        <button onclick="calculator.appendToDisplay('9')">9</button>
        <button onclick="calculator.appendToDisplay('*')">*</button>
        <button onclick="calculator.appendToDisplay('4')">4</button>
        <button onclick="calculator.appendToDisplay('5')">5</button>
        <button onclick="calculator.appendToDisplay('6')">6</button>
        <button onclick="calculator.appendToDisplay('-')">-</button>
        <button onclick="calculator.appendToDisplay('1')">1</button>
        <button onclick="calculator.appendToDisplay('2')">2</button>
        <button onclick="calculator.appendToDisplay('3')">3</button>
        <button onclick="calculator.appendToDisplay('+')">+</button>
        <button onclick="calculator.appendToDisplay('0')">0</button>
        <button onclick="calculator.appendToDisplay('.')">.</button>
        <button onclick="calculator.calculate()">=</button>
    </div>
</div>

<script>
    class Calculator {
        constructor() {
            this.displayElement = document.getElementById('display');
        }

        appendToDisplay(value) {
            this.displayElement.value += value;
        }

        clearDisplay() {
            this.displayElement.value = '';
        }

        calculate() {
            try {
                const result = eval(this.displayElement.value);
                if (isNaN(result) ||!isFinite(result)) {
                    this.displayElement.value = '错误';
                } else {
                    this.displayElement.value = result;
                }
            } catch (error) {
                this.displayElement.value = '错误';
            }
        }
    }

    const calculator = new Calculator();
</script>
</body>

</html>